package com.haliyoo.adhere.sqlite;

import com.haliyoo.adhere.bean.UserBean;
import com.haliyoo.adhere.bean.WeiboBean;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * 创建数据库
 * @author Administrator
 *
 */
public class SqliteHelper4Weibo extends SQLiteOpenHelper {
	private static final String TAG = "SqliteHelper4Weibo";
	public static final String TB_NAME = "adweibo";

	public SqliteHelper4Weibo(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
	}

	/// 创建表
	@Override
	public void onCreate(SQLiteDatabase arg0) {
		arg0.execSQL("CREATE TABLE IF NOT EXISTS "+
	                TB_NAME+"(" +
	                WeiboBean.ID + " integer primary key," +
	                WeiboBean.USER + " varchar," +
	                WeiboBean.PROFILE + "blob," + 
	                WeiboBean.WEIBOID + " long," +
	                WeiboBean.TEXT + " varchar," +
	                WeiboBean.EXISTICON + " short," +
	                WeiboBean.ICON + " blob," +
	                WeiboBean.EXISTLOCATION + " short," +
	                WeiboBean.LATITUDE + " double," +
	                WeiboBean.LONGITUDE + " double," +
	                WeiboBean.CREATEAT + " varchar" +
	                ")"
	                );
	        Log.i(TAG, "onCreate");
	}

	/// 更新表
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + TB_NAME);
        onCreate(db);
        Log.i(TAG, "onUpgrade");
	}

	/// 更新列
	public void updateColumn(SQLiteDatabase db, String oldColumn, String newColumn, String typeColumn){
        try{
            db.execSQL("ALTER TABLE " +
                    TB_NAME + " CHANGE " +
                    oldColumn + " "+ newColumn +
                    " " + typeColumn
            );
        }catch(Exception e){
            e.printStackTrace();
        }
    }

}
